import os
import shutil
import sys


def copy_and_rename_folder(template_folder, new_folder_name):
    # 创建新的目标文件夹路径
    new_folder_path = f"./{new_folder_name}"

    # 复制整个文件夹
    shutil.copytree(template_folder, new_folder_path)

    # 计算新的文件名（去掉前缀数字和 '-'）
    new_file_name = new_folder_name.split("-", 1)[-1]

    # 原始文件路径
    template_file_path = os.path.join(new_folder_path, "template.sv")

    # 新的文件路径
    new_file_path = os.path.join(new_folder_path, new_file_name + ".sv")

    # 重命名文件
    if os.path.exists(template_file_path):
        os.rename(template_file_path, new_file_path)
    else:
        print(f"File {template_file_path} does not exist")


if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python script.py <new_folder_name>")
        sys.exit(1)

    # 从命令行参数获取新的文件夹名称
    new_folder_name = sys.argv[1]

    # 源文件夹
    template_folder = "./0-template"

    # 执行复制和重命名操作
    copy_and_rename_folder(template_folder, new_folder_name)
